package ${groupId}.controller.boss.login;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ${groupId}.service.security.dto.LoginUserInfo;
import ${groupId}.service.system.menu.MenuService;
import ${groupId}.service.system.menu.dto.MenuNode;
import com.system.boss.shortcut.SysShortCutService;
import com.system.core.controller.ControllerBase;
import ${groupId}.service.system.sysparam.SysParamService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping(value = {"boss","merchant"})
@Slf4j
public class BossIndexController extends ControllerBase {
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private SysParamService sysParamService;
	
	@Autowired
	private SysShortCutService sysShortCutService;

	@RequestMapping(value = "main",method = RequestMethod.GET)
	public String mainPage() {
		LoginUserInfo loginUserInfo = (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
		getModelMap().addAttribute("sysmenu", getSystemMenuList(loginUserInfo.getRoleId()));
		getModelMap().addAttribute("boss_title",sysParamService.getString("boss_title"));
		return "/boss/index";
	}
	
	@RequestMapping(value = "console",method = RequestMethod.GET)
	public String console() {
		getModelMap().addAttribute("shortcuts", sysShortCutService.actived());
		return "/boss/console";
	}
	
	private List<MenuNode> getSystemMenuList(Long roleId) {
		List<MenuNode> list = menuService.getMenusByParentId(0,roleId==null?-999:roleId);
		// 菜单排个序，不要乱了
		Collections.sort(list, new Comparator<MenuNode>() {
			@Override
			public int compare(MenuNode o1, MenuNode o2) {
				if (o1.getSort() - o2.getSort() > 0)
					return 1;
				else
					return -1;
			}
		});
		
		for (int i = 0; i < list.size(); i++) {
			Collections.sort(list.get(i).getChildren(), new Comparator<MenuNode>() {
				@Override
				public int compare(MenuNode o1, MenuNode o2) {
					if (o1.getSort() - o2.getSort() > 0)
						return 1;
					else
						return -1;
				}
			});
		}
		return list;
	}
}
